package controllers;

import java.util.Date;
import java.io.File;

import org.apache.commons.io.FileUtils;


import com.opensymphony.xwork2.ActionSupport;
public class StrutsFileUpload extends ActionSupport {
  
  private File upload;//The actual file
  private String uploadContentType; //The content type of the file
  private String uploadFileName; //The uploaded file name
  private String fileCaption;//The caption of the file entered by user
  private int rand; // Permet de ne pas avoir 2 image avec le meme nom
  

public String execute() throws Exception {

	  try {
		  this.setRand((int)Math.floor(Math.random() * 5000000));
		  
		  String fullFileName = "D:/workspace/PDMHibernate/WebContent/jsp/upload/"+this.getRand()+"_"+this.getUploadFileName();

		  File theFile = new File(fullFileName);

		  FileUtils.copyFile(upload, theFile);

		  } catch (Exception e) {

		  addActionError(e.getMessage());

		  return INPUT;

		  }
	  
	  return SUCCESS;


  }
  public String getFileCaption() {
  return fileCaption;
  }
  public void setFileCaption(String fileCaption) {
  this.fileCaption = fileCaption;
  }
  public File getUpload() {
  return upload;
  }
  public void setUpload(File upload) {
  this.upload = upload;
  }
  public String getUploadContentType() {
  return uploadContentType;
  }
  public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
  }
  public String getUploadFileName() {
  return uploadFileName;
  }
  public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
  }
  public int getRand() {
	return rand;
	}
	public void setRand(int rand) {
		this.rand = rand;
	}

  
}